package com.java46.front.order.dao;

import com.java46.front.base.exception.BusinessException;
import com.java46.front.base.util.HibernateUtils;
import com.java46.front.order.entity.OrderinfoEntity;
import com.java46.front.order.entity.OrdersEntity;
import org.hibernate.Session;
import org.hibernate.query.Query;


import java.util.List;
public class OrderDao {
    //添加订单
    public void addOrders(OrdersEntity oe){
        Session session = HibernateUtils.getSession();
        session.save(oe);
        session.beginTransaction().commit();
        session.close();
    }

    //根据用户ID和下单时间得到一个Orders对象
    public OrdersEntity getOrders(int userId,String time){
        Session session = HibernateUtils.getSession();
        Query query =   HibernateUtils.getQuery("from com.java46.front.order.entity.OrdersEntity oe where oe.userId=?1 and oe.time=?2");
        query.setParameter(1,userId);
        query.setParameter(2,time);
        List<OrdersEntity> list = query.list();
        if(list.size()==0){
            throw new BusinessException("没有订单啊");
        }
        OrdersEntity ordersEntity = list.get(0);
        session.close();
        return ordersEntity;
    }
    //添加订单详情
    public void addOrderInfo(OrderinfoEntity of){
        Session session = HibernateUtils.getSession();
        session.save(of);
        session.beginTransaction().commit();
        session.close();
    }

}
